{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1.输入某年某月某日，判断这一天是这一年的第几天？\n",
    "year = int(input(\"请输入年分：\"))\n",
    "month = int(input(\"请输入月份：\"))\n",
    "day = int(input(\"请输入日：\"))\n",
    "print(\"-\" * 20)\n",
    "day += (month-1) * 30   \n",
    "if month < 9:   \n",
    "    day += month//2\n",
    "else: \n",
    "    day += (month+1)//2\n",
    " if month > 2:  \n",
    "    if year%400 ==0 or year%4 == 0 and year%100 !=0 :\n",
    "        day -= 1\n",
    "    else:\n",
    "        day -= 2\n",
    "print(\"是一年的第%d天\"%day)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "2.输出9x9乘法口诀表。\n",
    "a=[1,2,3,4,5,6,7,8,9]\n",
    "for i in range(len(a)):\n",
    "    for j in range(i+1):\n",
    "        print(\"%d*%d=\"%(a[j],a[i]),a[j]*a[i],end=\"| \")\n",
    "    print(\" \")\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "3.判断1000中有多少个素数，并输出所有素数。\n",
    "from math import sqrt\n",
    "def is_prime(n):\n",
    "if n == 1:\n",
    "return False\n",
    "for i in range(2, int(sqrt(n))+1):\n",
    "if n % i == 0:\n",
    "return False\n",
    "return True\n",
    "count = 0\n",
    "for i in range(1, 1000):\n",
    "if is_prime(i):\n",
    "count = count + 1\n",
    "print('{}:{}'.format(count, i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "4.输入一个任意正整数，输出分解质因数。例如：输入90,打印出90=2*3*3*5。\n",
    "public static void main(String[] args) {\n",
    "Scanner sc = new Scanner(System.in);\n",
    "System.out.println(\"请输入数字：\");\n",
    "int n = sc.nextInt();\n",
    "System.out.print(n+\"=\"); \n",
    "    for(int k=2;k<=n;k++) {    \n",
    "        while(n!=k) {\n",
    "            if(n%k==0) {\n",
    "                System.out.print(k+\"*\");\n",
    "                n=n/k;\n",
    "                else{\n",
    "                    break;\n",
    "                    System.out.println(n); \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "5.利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。\n",
    "    main()\n",
    "    {\n",
    "        int score;\n",
    "        char grade;\n",
    "        printf(\"please input a score\\n\");\n",
    "        scanf(\"%d\",&score);\n",
    "        grade=score>=90?'A'score>=60?'B':'C');\n",
    "        printf(\"%d belongs to %c\",score,grade);\n",
    "    }\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "6.输入一个不多于6位的正整数，要求：一、求它是几位数，二、逆序打印出各位数字。\n",
    "int main( )\n",
    "{\n",
    "    long a,b,c,d,e,x;\n",
    "    printf(\"请输入 6 位数字：\");\n",
    "    scanf(\"%ld\",&x);\n",
    "    a=x/100000:\n",
    "    b=x/10000;        \n",
    "    c=x%10000/1000;   \n",
    "    d=x%1000/100;    \n",
    "    e=x%100/10;       \n",
    "    f=x%10;           \n",
    "    if (a!=0){\n",
    "        printf(\"这个数为 6 位数,逆序为：%ld %ld %ld %ld %ld %ld\\n\",f,e,d,c,b,a);\n",
    "    } else if(b!=0) {\n",
    "         printf(\"这个数为 4 位数,逆序为： %ld %ld %ld %ld\\n\",f,e,d,c,b);\n",
    "    } else if(c!=0) {\n",
    "         printf(\"这个数为 3 位数,逆序为：%ld %ld %ld\\n\",f,e,d,c);\n",
    "    } else if(d!=0) {\n",
    "         printf(\"这个数为 2 位数,逆序为： %ld %ld\\n\",f,e,d);\n",
    "    } else if(e!=0) {\n",
    "         printf(\"这个数为 1 位数,逆序为：%ld\\n\",f,e);\n",
    "    } else if(f!=0) {\n",
    "         printf(\"这个数为 1 位数,逆序为：%ld\\n\",f);\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "7.输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数。\n",
    "int main()\n",
    "{\n",
    "    char c;\n",
    "    int letters=0,spaces=0,digits=0,others=0;\n",
    "    printf(\"请输入一串任意的字符：\\n\");\n",
    "    while((c=getchar())!='\\n')\n",
    "    {\n",
    "        if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))\n",
    "        letters++;\n",
    "        else if(c>='0'&&c<='9')\n",
    "        digits++;\n",
    "        else if(c==' ')\n",
    "        spaces++;\n",
    "        else\n",
    "        others++;\n",
    "    }\n",
    "    printf(\"字母有%d个，数字有%d个，空格有%d个，其他有%d个\",letters,digits,spaces,others);\n",
    "    return 0;\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "8.求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)，几个数相加有键盘控制。\n",
    "public static void main(String[] args) {\n",
    "        Scanner sca = new Scanner(System.in);\n",
    "        System.out.println(\"请输入一个数\");\n",
    "        int num = sca.nextInt();\n",
    "        int n=0, sum =0;\n",
    "        for (int i = 1; i <= 5; i++) {\n",
    "            n = n*10 + num;\n",
    "            sum=sum+n;\n",
    "        }\n",
    "        System.out.println(sum);\n",
    "    }\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "9.一个数如果恰好等于它的因子之和，这个数就称为“完数”。例如6=1＋2＋3.编程，找出1000以内的所有完数。\n",
    "nt main() {\n",
    "for (int i = 2; i <= 1000; i++) {//从2到1000的数\n",
    "int sum = 0;//因子总和\n",
    "for (int j = 1; (j * j) <= i; j++) {//j做除数，√n复杂度，减少一半的计算量\n",
    "if (i % j == 0) {//可以整除，为其因子\n",
    "sum = j + (i / j) + sum;//因子之和\n",
    "}\n",
    " if ((sum - i) == i) cout << \"1000以内的完数：\" << i << endl;//因子之和减去其本身（1*i也为其因子）如果等于这个数本身，则为完数}\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "10.有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13...求出这个数列的前N项之和，N由键盘输入。\n",
    "int main()\n",
    "{\n",
    "    int n;\n",
    "    printf(\"请输入你想计算前多少项的和：\\n\");\n",
    "    scanf(\"%d\",&n);\n",
    "    int i,t;\n",
    "    float sum=0;\n",
    "    float a=2,b=1;\n",
    "    for(i=1;i<=n;i++)\n",
    "    {\n",
    "        sum=sum+a/b;\n",
    "        t=a;\n",
    "        a=a+b;\n",
    "        b=t;\n",
    "    }\n",
    "    printf(\"前%d项的和为%9.6f\\n\",n,sum);\n",
    "}\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
